<div class="panel panel-default panel-intro">
    <div class="panel-heading">
        <h3 class="panel-title">批量审核主动报工</h3>
    </div>
    <div class="panel-body">
        <form id="batchAuditForm" method="post">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label>审核结果 <span class="text-danger">*</span></label>
                        <div>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="1" required> 通过
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="status" value="2" required> 拒绝
                            </label>
                        </div>
                    </div>
                    
                    <div class="form-group">
                        <label>审核原因</label>
                        <textarea name="audit_reason" class="form-control" rows="4" placeholder="请输入审核原因（可选）"></textarea>
                    </div>
                </div>
                
                <div class="col-md-6">
                    <div class="form-group">
                        <label>选中的记录</label>
                        <div id="selectedRecords" class="well" style="max-height: 200px; overflow-y: auto;">
                            <p class="text-muted">请先选择要审核的记录</p>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="form-group">
                <button type="submit" class="btn btn-success">
                    <i class="fa fa-check"></i> 提交批量审核
                </button>
                <a href="javascript:history.back()" class="btn btn-default">
                    <i class="fa fa-arrow-left"></i> 返回
                </a>
            </div>
        </form>
    </div>
</div>

<script>
$(document).ready(function() {
    // 获取选中的记录
    var selectedIds = Fast.api.getSelectedIds();
    if (!selectedIds || selectedIds.length === 0) {
        Toastr.error('请先选择要审核的记录');
        setTimeout(function() {
            location.href = 'scanwork/active_report/index';
        }, 1500);
        return;
    }
    
    // 显示选中的记录
    showSelectedRecords(selectedIds);
    
    // 表单提交
    $('#batchAuditForm').on('submit', function(e) {
        e.preventDefault();
        
        var status = $('input[name="status"]:checked').val();
        if (!status) {
            Toastr.error('请选择审核结果');
            return;
        }
        
        var auditReason = $('textarea[name="audit_reason"]').val();
        
        if (confirm('确定要对选中的 ' + selectedIds.length + ' 条记录进行批量审核吗？')) {
            var btn = $(this).find('button[type=submit]');
            var originalText = btn.html();
            
            btn.html('<i class="fa fa-spinner fa-spin"></i> 提交中...').prop('disabled', true);
            
            $.ajax({
                                    url: 'scanwork/active_report/batchAudit',
                method: 'POST',
                data: {
                    ids: selectedIds.join(','),
                    status: status,
                    audit_reason: auditReason
                },
                success: function(response) {
                    if (response.code === 1) {
                        Toastr.success('批量审核完成');
                                                        setTimeout(function() {
                                    location.href = 'scanwork/active_report/index';
                                }, 1500);
                    } else {
                        Toastr.error(response.msg || '批量审核失败');
                        btn.html(originalText).prop('disabled', false);
                    }
                },
                error: function() {
                    Toastr.error('网络错误，请重试');
                    btn.html(originalText).prop('disabled', false);
                }
            });
        }
    });
});

function showSelectedRecords(ids) {
    $.ajax({
        url: 'scanwork/active_report/getSelectedRecords',
        method: 'GET',
        data: { ids: ids.join(',') },
        success: function(response) {
            if (response.code === 1) {
                var html = '';
                response.data.forEach(function(record) {
                    html += '<div class="record-item">';
                    html += '<strong>' + record.order_no + '</strong> - ';
                    html += record.user_name + ' - ';
                    html += record.product_name + ' - ' + record.model_name + ' - ';
                    html += record.quantity + '件';
                    html += '</div>';
                });
                $('#selectedRecords').html(html);
            } else {
                $('#selectedRecords').html('<p class="text-danger">获取记录信息失败</p>');
            }
        },
        error: function() {
            $('#selectedRecords').html('<p class="text-danger">网络错误</p>');
        }
    });
}
</script> 